﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace Database
{
    /// <summary>
    /// 装备数据库
    /// </summary>
    public class EquipDatabase
    {
        private static Equip Equip海市腿甲 = new Equip("海市腿甲",new byte[]{0xE7,0xD7,0x63,0x00});
        private static Equip Equip海市胸甲 = new Equip("海市胸甲", new byte[] { 0x8A, 0xDD, 0x63, 0x00 });
        private static Equip Equip海市戒指 = new Equip("海市戒指", new byte[] { 0xC4, 0xD7, 0x63, 0x00 });
        private static Equip Equip海市之鞭 = new Equip("海市之鞭", new byte[] { 0xBA, 0xD8, 0x63, 0x00 });
        private static Equip Equip海市之弓 = new Equip("海市之弓", new byte[] { 0xBA, 0xD8, 0x63, 0x00 });
        private static Equip Equip山海之剑 = new Equip("山海之剑", new byte[] { 0x2F, 0xDC, 0x63, 0x00 });
        private static Equip Equip坚固肩甲 = new Equip("坚固肩甲", new byte[] { 0xEB, 0xD8, 0x63, 0x00 });
        private static Equip Equip焚骨项链 = new Equip("焚骨项链", new byte[] { 0x39, 0xDB, 0x63, 0x00 });
        private static Equip Equip山河腿甲 = new Equip("山河腿甲", new byte[] { 0x03, 0xD9, 0x63, 0x00 });

        private static Dictionary<string, Equip> equipDict = new Dictionary<string, Equip>(256);
        private static List<Equip> equipList = null;

        public static void Init()
        {
            Console.WriteLine("开始加载装备数据库...");
            Load();
            if (equipList == null)
            {
                #region 程序内置装备数据
                equipList = new List<Equip>(256);
                equipList.Add(Equip海市腿甲);
                equipList.Add(Equip海市戒指);
                equipList.Add(Equip海市之鞭);
                equipList.Add(Equip海市胸甲);
                equipList.Add(Equip海市之弓);
                equipList.Add(Equip山海之剑);
                equipList.Add(Equip坚固肩甲);
                equipList.Add(Equip焚骨项链);
                equipList.Add(Equip山河腿甲);
                #endregion

                Save();
            }

            foreach (Equip m in equipList)
            {
                equipDict.Add(m.Name, m);
                Console.WriteLine("装备："+m.Name+"\t LV"+m.NeedLV+"\tCode:"+m.CodeString);
            }
            Console.WriteLine("加载装备数据库完成！");
        }

        /// <summary>
        /// 根据怪物名称获取怪物
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Equip GetEquip(string name)
        {
            return equipDict[name];
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//data//equip.xml";
            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Equip>));
                    serializer.Serialize(stream, equipList);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("保存装备数据库出错，错误信息：");
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public static void Load()
        {
            try
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\data\\equip.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Equip>));
                    equipList = (List<Equip>)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("加载装备数据库出错，已采用程序内置数据，错误信息：");
                Tools.PrintException(ex);
            }
        }
    }
}
